﻿using FluentAssertions.Execution;
using OneOf.Types;
using Org.BouncyCastle.Asn1.Mozilla;
using QRCoder;
using System;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using ZXing;
using ZXing.Common;
using ZXing.Datamatrix;
using ZXing.OneD;
using ZXing.Rendering;
using static QRCoder.QRCodeGenerator;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    /// <summary>
    /// 引用 QRCode 类的工具方法。
    /// </summary>
    public class QRBuilder
    {
        /// <summary>
        /// 创建二维码。
        /// </summary>
        /// <param name="sourceText">要编码的字符串。</param>
        /// <param name="eccLevel">尺寸。</param>
        /// <param name="iconBitmap">中间的图标。</param>
        /// <param name="iconSize">中间图标的尺寸。</param>
        /// <returns>返回Bitmap对象。</returns>
        public static System.Drawing.Bitmap BuildQrCodeBitmap(string sourceText,
            ECCLevel eccLevel, Bitmap iconBitmap, int iconSize, bool encodeUrl, bool transparent)
        {
            if (string.IsNullOrEmpty(sourceText)) return null;

            if (encodeUrl && (sourceText.StartsWith("https://") || sourceText.StartsWith("http://")))
            {
                // 进行 Url 编码，这样可以解决微信扫码时自动移除中文字符的问题。
                sourceText = UrlEncode(sourceText);
            }

            Bitmap resultBitmap;
            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
            {
                using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(sourceText, eccLevel))
                {
                    using (QRCode qrCode = new QRCode(qrCodeData))
                    {
                        resultBitmap = qrCode.GetGraphic(20, Color.Black, Color.White, iconBitmap, iconSize);
                    }
                }
            }

            if (transparent)
            {
                resultBitmap.MakeTransparent(Color.FromArgb(255,255,255,255));
            }

            return resultBitmap;
        }

        public static Bitmap 生成二维码文件(string destImagePath, string sourceText,
                string eccLevelTxt, string iconPath, int iconSize, bool encodeUrl)
        {
            if (string.IsNullOrEmpty(destImagePath)) return null;
            if (string.IsNullOrEmpty(sourceText)) return null;


            var destfi = new FileInfo(destImagePath);
            var eccLevel = ECCLevel.Default;
            if (string.IsNullOrWhiteSpace(eccLevelTxt) == false)
            {
                switch (eccLevelTxt.ToLower().Trim())
                {
                    case "l":
                        {
                            eccLevel = ECCLevel.L; break;
                        }
                    case "q":
                        {
                            eccLevel = ECCLevel.Q; break;
                        }
                    case "h":
                        {
                            eccLevel = ECCLevel.H; break;
                        }
                    case "m":
                        {
                            eccLevel = ECCLevel.M; break;
                        }
                }
            }

            Bitmap iconBmp = null;
            if (File.Exists(iconPath))
            {
                iconBmp = new Bitmap(iconPath);
            }

            var bmp = BuildQrCodeBitmap(sourceText, eccLevel, iconBmp, iconSize, encodeUrl, true);
            bmp.Save(destfi.FullName);
            return bmp;
        }

        /// <summary>
        /// 将 Url 中的汉字等进行转义。
        /// </summary>
        /// <param name="sourceText">源文本。</param>
        /// <returns></returns>
        public static string UrlEncode(string sourceText)
        {
            if (string.IsNullOrEmpty(sourceText)) return "";

            var sb = new StringBuilder();
            foreach (char c in sourceText)
            {
                if ((int)c <= 127)
                {
                    sb.Append(c); continue;
                }

                sb.Append(System.Web.HttpUtility.UrlEncode(c.ToString()));
            }
            return sb.ToString();
        }

        public static string DecodeQRCodeByZXing(string imagePath, bool crop = true)
        {
            using (var bitmap = new Bitmap(imagePath))
            {
                var result = DecodeQRCodeByZXing(bitmap);
                return result;
            }
        }

        public static string DecodeQRCodeByZXing(Bitmap bitmap, bool crop = true)
        {
            var barcodeReader = new BarcodeReader<Bitmap>(bm => new BitmapLuminanceSource(bm));
            var result = barcodeReader.Decode(bitmap);
            if (result != null)
            {
                return result.Text;
            }

            // 依次裁切左上、右上、左下、右下四个区域并尝试识别；
            // 如果还不行，按九宫格继续尝试；
            // 如果依然不行，最后尝试将图切分成四块再尝试。
            if (crop)
            {
                var resultTxt = DecodeQRCodeByZXingAtBorder(bitmap);
                if (string.IsNullOrEmpty(resultTxt) || resultTxt == "无法识别。")
                {
                    resultTxt = DecodeQRCodeByZXingAtNineSquareGrid(bitmap);
                    if (string.IsNullOrEmpty(resultTxt) || resultTxt == "无法识别。")
                    {
                        resultTxt = DecodeQRCodeByZXingAtFourSquareGrid(bitmap);
                    }
                }
            }

            return "无法识别。";
        }

        private static string DecodeQRCodeByZXingAtBorder(Bitmap bitmap)
        {
            using (var leftMap = CropImage(bitmap, new Rectangle(0, 0, bitmap.Width / 4, bitmap.Height)))
            {
                var leftResult = DecodeQRCodeByZXing(leftMap, false);
                if (leftResult != null && leftResult != "无法识别。") return leftResult;
            }

            using (var topMap = CropImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height / 4)))
            {
                var topResult = DecodeQRCodeByZXing(topMap, false);
                if (topResult != null && topResult != "无法识别。") return topResult;
            }

            using (var rightMap = CropImage(bitmap, new Rectangle(bitmap.Width / 4 * 3, 0, bitmap.Width / 4, bitmap.Height)))
            {
                var rightResult = DecodeQRCodeByZXing(rightMap, false);
                if (rightResult != null && rightResult != "无法识别。") return rightResult;
            }

            using (var bottomMap = CropImage(bitmap, new Rectangle(0, bitmap.Height / 4 * 3, bitmap.Width, bitmap.Height / 4)))
            {
                var rightbottomResult = DecodeQRCodeByZXing(bottomMap, false);
                if (rightbottomResult != null && rightbottomResult != "无法识别。") return rightbottomResult;
            }

            return "无法识别。";
        }

        private static string DecodeQRCodeByZXingAtFourSquareGrid(Bitmap bitmap)
        {
            int halfX = bitmap.Width / 2;
            int halfY = bitmap.Height / 2;

            using (var aMap = CropImage(bitmap, new Rectangle(0, 0, halfX, halfY)))
            {
                var aResult = DecodeQRCodeByZXing(aMap, false);
                if (aResult != null && aResult != "无法识别。") return aResult;
            }
            using (var bMap = CropImage(bitmap, new Rectangle(halfX, 0, halfX, halfY)))
            {
                var bResult = DecodeQRCodeByZXing(bMap, false);
                if (bResult != null && bResult != "无法识别。") return bResult;
            }
            using (var cMap = CropImage(bitmap, new Rectangle(0, halfY, halfX, halfY)))
            {
                var cResult = DecodeQRCodeByZXing(cMap, false);
                if (cResult != null && cResult != "无法识别。") return cResult;
            }
            using (var dMap = CropImage(bitmap, new Rectangle(halfX, halfY, halfX, halfY)))
            {
                var dResult = DecodeQRCodeByZXing(dMap, false);
                if (dResult != null && dResult != "无法识别。") return dResult;
            }
            return "无法识别。";
        }

        private static string DecodeQRCodeByZXingAtNineSquareGrid(Bitmap bitmap)
        {
            int x1_3 = bitmap.Width / 3;
            int x2_3 = bitmap.Width / 3 * 2;
            int y1_3 = bitmap.Height / 3;
            int y2_3 = bitmap.Height / 3 * 2;

            using (var aMap = CropImage(bitmap, new Rectangle(0, 0, x1_3, y1_3)))
            {
                var aResult = DecodeQRCodeByZXing(aMap, false);
                if (aResult != null && aResult != "无法识别。") return aResult;
            }
            using (var bMap = CropImage(bitmap, new Rectangle(x1_3, 0, x1_3, y1_3)))
            {
                var bResult = DecodeQRCodeByZXing(bMap, false);
                if (bResult != null && bResult != "无法识别。") return bResult;
            }
            using (var cMap = CropImage(bitmap, new Rectangle(x2_3, 0, x1_3, y1_3)))
            {
                var cResult = DecodeQRCodeByZXing(cMap, false);
                if (cResult != null && cResult != "无法识别。") return cResult;
            }
            using (var dMap = CropImage(bitmap, new Rectangle(0, y1_3, x1_3, y1_3)))
            {
                var dResult = DecodeQRCodeByZXing(dMap, false);
                if (dResult != null && dResult != "无法识别。") return dResult;
            }
            using (var eMap = CropImage(bitmap, new Rectangle(x1_3, y1_3, x1_3, y1_3)))
            {
                var eResult = DecodeQRCodeByZXing(eMap, false);
                if (eResult != null && eResult != "无法识别。") return eResult;
            }
            using (var fMap = CropImage(bitmap, new Rectangle(x2_3, y1_3, x1_3, y1_3)))
            {
                var fResult = DecodeQRCodeByZXing(fMap, false);
                if (fResult != null && fResult != "无法识别。") return fResult;
            }
            using (var gMap = CropImage(bitmap, new Rectangle(0, y2_3, x1_3, y1_3)))
            {
                var gResult = DecodeQRCodeByZXing(gMap, false);
                if (gResult != null && gResult != "无法识别。") return gResult;
            }
            using (var hMap = CropImage(bitmap, new Rectangle(x1_3, y2_3, x1_3, y1_3)))
            {
                var hResult = DecodeQRCodeByZXing(hMap, false);
                if (hResult != null && hResult != "无法识别。") return hResult;
            }
            using (var iMap = CropImage(bitmap, new Rectangle(x2_3, y2_3, x1_3, y1_3)))
            {
                var iResult = DecodeQRCodeByZXing(iMap, false);
                if (iResult != null && iResult != "无法识别。") return iResult;
            }
            return "无法识别。";
        }

        public static Bitmap CropImage(Bitmap source, Rectangle section)
        {
            // 创建一个与裁剪区域大小相同的新位图
            Bitmap target = new Bitmap(section.Width, section.Height);

            // 从原始位图中提取指定区域并绘制到新位图上
            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(source, new Rectangle(0, 0, section.Width, section.Height), section, GraphicsUnit.Pixel);
            }

            return target;
        }

        public static Bitmap CropImage(Bitmap source, double left, double top, double width, double height)
        {
            if (source == null) return null;

            if (left < 0) left = 0;
            if (top < 0) top = 0;
            if (width < 0) width = 0;
            if (height < 0) height = 0;

            if (left >= 0 && left <= 1)  // [0, 1] 区间内转为百分比
            {
                left = 0;
            }
            else if (left > 1 && left <= 100)  // (1, 100] 区间内视为 直接百分比
            {
                left /= 100;
            }
            else left = 1;

            if (top >= 0 && top <= 1)  // [0, 1] 区间内转为百分比
            {
                top = 0;
            }
            else if (top > 1 && top <= 100)  // (1, 100] 区间内视为 直接百分比
            {
                top /= 100;
            }
            else top = 1;

            if (width >= 0 && width <= 1)  // [0, 1] 区间内转为百分比
            {
                width = 0;
            }
            else if (width > 1 && width <= 100)  // (1, 100] 区间内视为 直接百分比
            {
                width /= 100;
            }
            else width = 1;

            if (height >= 0 && height <= 1)  // [0, 1] 区间内转为百分比
            {
                height = 0;
            }
            else if (height > 1 && height <= 100)  // (1, 100] 区间内视为 直接百分比
            {
                height /= 100;
            }
            else height = 1;

            var bw = source.Width;
            var bh = source.Height;

            var rect = new Rectangle((int)(bw * left), (int)(bh * height), (int)(bw * width), (int)(bh * width));
            return CropImage(source, rect);
        }

        public static IronPython.Runtime.PythonList DecodeMultipleQRCodesByZXing(Bitmap barcodeBitmap)
        {
            var barcodeReader = new BarcodeReader();

            // 扫描图像中的所有二维码
            var results = barcodeReader.DecodeMultiple(barcodeBitmap);
            if (results == null)
            {
                return null;
            }

            var list = new IronPython.Runtime.PythonList();
            foreach (var result in results)
            {
                list.Add(result.Text);
            }
            return list;
        }

        public static IronPython.Runtime.PythonList DecodeMultipleQRCodesByZXing(string pathToImage)
        {
            var barcodeReader = new BarcodeReader();
            var barcodeBitmap = new Bitmap(pathToImage);

            // 扫描图像中的所有二维码
            var results = barcodeReader.DecodeMultiple(barcodeBitmap);
            if (results == null)
            {
                return null;
            }

            var list = new IronPython.Runtime.PythonList();
            foreach (var result in results)
            {
                list.Add(result.Text);
            }
            return list;
        }

        public static string 识别指定区域(string imagePath, double left, double top, double width, double height, string codeType = "")
        {
            if (File.Exists(imagePath) == false) return null;

            using (var bitmap = new Bitmap(imagePath))
            {
                return 识别指定区域(bitmap, left, top, width, height, codeType);
            }
        }

        public static string 识别指定区域(Bitmap bitmap, double left, double top, double width, double height, string codeType = "")
        {
            using (var newBitmap = CropImage(bitmap, left, top, width, height))
            {
                var result = DecodeQRCodeByZXing(newBitmap);
                if (string.IsNullOrEmpty(result) == false) return result;

                return 识别条形码(newBitmap);
            }
        }



        /// <summary>
        /// 生成条形码。
        /// </summary>
        /// <param name="text">条形码文本。</param>
        /// <param name="format">格式，39或128</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap GenerateBarcode(string text, BarcodeFormat format, int width, int height, int margin = 1)
        {
            var writer = new BarcodeWriter
            {
                Format = format,
                Options = new EncodingOptions
                {
                    Width = width,
                    Height = height,
                    Margin = margin, //设置边距，可以根据需要调整
                },
                Renderer = new BitmapRenderer() // 使用BitmapRenderer生成Bitmap图像
            };

            return writer.Write(text);
        }

        public static Bitmap 生成39条形码(string text, int width = 440, int height = 310, int margin = 1)
        {
            return GenerateBarcode(text, BarcodeFormat.CODE_39, width, height, margin);
        }

        public static Bitmap 生成128条形码(string text, int width = 440, int height = 310, int margin = 1)
        {
            return GenerateBarcode(text, BarcodeFormat.CODE_128, width, height, margin);
        }

        public static Bitmap 生成矩阵条形码(string text, int width = 440, int height = 440, int margin = 1)
        {
            return GenerateBarcode(text, BarcodeFormat.DATA_MATRIX, width, height, margin);
        }

        public static Bitmap 生成pdf417条形码(string text, int width = 440, int height = 310, int margin = 1)
        {
            return GenerateBarcode(text, BarcodeFormat.PDF_417, width, height, margin);
        }

        public static Bitmap 生成ean13条形码(string text, int width = 440, int height = 310, int margin = 1)
        {
            return GenerateBarcode(text, BarcodeFormat.EAN_13, width, height, margin);
        }

        public static Bitmap 生成39条形码文件(string destFilePath, string text, int width = 440, int height = 310, int margin = 1)
        {
            var bitmap = GenerateBarcode(text, BarcodeFormat.CODE_39, width, height, margin);
            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成128条形码文件(string destFilePath, string text, int width = 440, int height = 310, int margin = 1)
        {
            var bitmap = GenerateBarcode(text, BarcodeFormat.CODE_128, width, height, margin);
            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成矩阵条形码文件(string destFilePath, string text, int width = 440, int height = 440, int margin = 1)
        {
            var w = new DataMatrixWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.DATA_MATRIX, width, height);
            var bitmap = render.Render(m, BarcodeFormat.DATA_MATRIX, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成pdf417条形码文件(string destFilePath, string text, int width = 440, int height = 310, int margin = 1)
        {
            var bitmap = GenerateBarcode(text, BarcodeFormat.PDF_417, width, height, margin);
            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成ean13条形码文件(string destFilePath, string text, int width = 440, int height = 310, int margin = 1)
        {
            var w = new EAN13Writer();
            var render = new AlternateBitmapRenderer();
            // EAN-13 商品条码的标准尺寸是 37.29mm * 26.26mm，按 300DPI 换算像素尺寸是 440 * 310
            var m = w.encode(text, BarcodeFormat.EAN_13, width, height);
            var bitmap = render.Render(m, BarcodeFormat.EAN_13, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成aztec条形码(string text, int width = 440, int height = 440, int margin = 1)
        {
            var w = new ZXing.Aztec.AztecWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.AZTEC, width, height);
            var bitmap = render.Render(m, BarcodeFormat.AZTEC, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            return bitmap;
        }

        public static Bitmap 生成aztec条形码文件(string destFilePath, string text, int width = 440, int height = 440, int margin = 1)
        {
            var w = new ZXing.Aztec.AztecWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.AZTEC, width, height);
            var bitmap = render.Render(m, BarcodeFormat.AZTEC, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成codabar条形码(string text, int width = 440, int height = 220, int margin = 1)
        {
            var w = new CodaBarWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.CODABAR, width, height);
            var bitmap = render.Render(m, BarcodeFormat.CODABAR, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成codabar条形码文件(string destFilePath, string text, int width = 440, int height = 220, int margin = 1)
        {
            var w = new CodaBarWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.CODABAR, width, height);
            var bitmap = render.Render(m, BarcodeFormat.CODABAR, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成code93条形码(string text, int width = 440, int height = 310, int margin = 1)
        {
            var w = new Code93Writer();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.CODE_93, width, height);
            var bitmap = render.Render(m, BarcodeFormat.CODE_93, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成code93条形码文件(string destFilePath, string text, int width = 440, int height = 310, int margin = 1)
        {
            var w = new Code93Writer();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.CODE_93, width, height);
            var bitmap = render.Render(m, BarcodeFormat.CODE_93, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成ean8条形码(string text, int width = 440, int height = 440, int margin = 1)
        {
            var w = new EAN8Writer();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.EAN_8, width, height);
            var bitmap = render.Render(m, BarcodeFormat.EAN_8, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成ean8条形码文件(string destFilePath, string text, int width = 440, int height = 440, int margin = 1)
        {
            var w = new EAN8Writer();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.EAN_8, width, height);
            var bitmap = render.Render(m, BarcodeFormat.EAN_8, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成itf条形码(string text, int width = 440, int height = 400, int margin = 1)
        {
            var w = new ITFWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.ITF, width, height);
            var bitmap = render.Render(m, BarcodeFormat.ITF, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成itf条形码文件(string destFilePath, string text, int width = 440, int height = 400, int margin = 1)
        {
            var w = new ITFWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.ITF, width, height);
            var bitmap = render.Render(m, BarcodeFormat.ITF, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成upca条形码(string text, int width = 440, int height = 240, int margin = 1)
        {
            var w = new UPCAWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.UPC_A, width, height);
            var bitmap = render.Render(m, BarcodeFormat.UPC_A, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成upca条形码文件(string destFilePath, string text, int width = 440, int height = 240, int margin = 1)
        {
            var w = new UPCAWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.UPC_A, width, height);
            var bitmap = render.Render(m, BarcodeFormat.UPC_A, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成upce条形码(string text, int width = 440, int height = 240, int margin = 1)
        {
            var w = new UPCEWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.UPC_E, width, height);
            var bitmap = render.Render(m, BarcodeFormat.UPC_E, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成upce条形码文件(string destFilePath, string text, int width = 440, int height = 240, int margin = 1)
        {
            var w = new UPCEWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.UPC_E, width, height);
            var bitmap = render.Render(m, BarcodeFormat.UPC_E, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static Bitmap 生成plessey条形码(string text, int width = 440, int height = 160, int margin = 1)
        {
            var w = new PlesseyWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.PLESSEY, width, height);
            var bitmap = render.Render(m, BarcodeFormat.PLESSEY, text, new EncodingOptions { Width = width, Height = height, Margin = margin });
            return bitmap;
        }

        public static Bitmap 生成plessey条形码文件(string destFilePath, string text, int width = 440, int height = 160, int margin = 1)
        {
            var w = new PlesseyWriter();
            var render = new AlternateBitmapRenderer();
            var m = w.encode(text, BarcodeFormat.PLESSEY, width, height);
            var bitmap = render.Render(m, BarcodeFormat.PLESSEY, text, new EncodingOptions { Width = width, Height = height, Margin = margin });

            bitmap.Save(destFilePath);
            return bitmap;
        }

        public static string 识别条形码(string filePath)
        {
            var reader = new BarcodeReader();
            var result = reader.Decode(new Bitmap(filePath));

            if (result == null)
                return "未能识别。";

            return result.Text;
        }

        public static string 识别条形码(Bitmap bitmap)
        {
            var reader = new BarcodeReader();
            var result = reader.Decode(bitmap);

            if (result == null)
                return "未能识别。";

            return result.Text;
        }
    }

    public static class BitmapTools
    {
        public static Bitmap ResizeImage(this Bitmap original, int newWidth, int newHeight)
        {
            // 创建一个新的Bitmap，其尺寸为新的宽度和高度
            Bitmap resized = new Bitmap(newWidth, newHeight);

            using (Graphics g = Graphics.FromImage(resized))
            {
                // 设置绘图质量
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(original, 0, 0, newWidth, newHeight);
            }

            return resized;
        }

        public static Bitmap 置图像尺寸(this Bitmap original, int newWidth, int newHeight)
        {
            return original.ResizeImage(newWidth, newHeight);
        }
    }
}
